package com.yijietong.yiliao.doctor.network.rxjava;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

/**
 * Created by Administrator
 * on 2016/5/18.
 */
public class ObserverCallBack<T> implements Observer<T> {
    private ApiCallback<T> apiCallback;
    private Disposable disposable;

    public ObserverCallBack(ApiCallback<T> apiCallback) {
        this.apiCallback = apiCallback;
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            //httpException.response().errorBody().string()
            int code = httpException.code();
            String msg = httpException.getMessage();
            if (code == 504) {
                msg = "网络不给力，请稍候再试";
            }
            apiCallback.onFailure(msg);
        } else {
            apiCallback.onFailure("网络不给力，请稍候再试!!");
        }
    }

    @Override
    public void onComplete() {
        apiCallback.onComplete();
    }

    @Override
    public void onSubscribe(Disposable disposable) {
        this.disposable = disposable;
    }

    @Override
    public void onNext(T t) {
            apiCallback.onSuccess(t);
    }
}
